package com.huihui.activity.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.huihui.R;
import com.huihui.activity.CommentListActivity;
import com.huihui.adapter.MineMsgZanAdapter;
import com.huihui.adapter.MineZanAdapter;
import com.huihui.base.BaseActivity;
import com.huihui.bean.Goods;
import com.huihui.bean.MinePingLun;
import com.huihui.datamanager.AppManager;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.widget.SwipeRecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 我的消息-赞
 */
public class MineMsgZanActivity extends BaseActivity {

    private SwipeRecyclerView recyclerView;
    private MineMsgZanAdapter adapter;
    private List<MinePingLun> mList = new ArrayList<>();

    private int pageIndex = 1;
    private int pageNum = 10;
    private MinePingLun currentPingLun;
    private LinearLayout inputLayout;
    private EditText inputText;
    private MinePingLun pingLun;
    private int class_id = 0;
    private long relation_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_msg_zan);
//        getSupportActionBar().setTitle(R.string.zan);
        titleView.setText(R.string.zan);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        recyclerView.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                pageIndex = 1;
                initData();
            }

            @Override
            public void onLoadMore() {
                pageIndex++;
                initData();
            }
        });

        adapter.setOnDianZanClickListener(new MineMsgZanAdapter.OnDianZanListener() {
            @Override
            public void onClick(int position) {
//                pingLun = mList.get(position);
//                class_id = pingLun.getClass_id();
//                relation_id = pingLun.getRelation_id();
//                showInputKeyboard();

            }
        });

        inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String content = v.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    if (pingLun != null) {
                        replyComment(content, pingLun.getCurrent_comment_id());
                    }
                    hideInputKeyboard();
                }

                return false;
            }
        });

    }

    private void initData() {
        if (getUserInfo() == null) {
            return;
        }
        OkHttpRequest.getInstance().msgList(HttpContants.msg_list, getUserInfo().getUser_id(), 2, 20, pageIndex, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                recyclerView.setRefreshing(false);
            }

            @Override
            public void onResponse(String response) {
                recyclerView.setRefreshing(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject != null && jsonObject.has(HttpContants.DATA)) {
                        List<MinePingLun> list = AppManager.getGson().fromJson(jsonObject.getString(HttpContants.DATA), new TypeToken<List<MinePingLun>>() {
                        }.getType());
                        if (list != null) {
                            if (pageIndex == 1) {
                                mList.clear();
                                mList.addAll(list);
                                adapter.notifyDataSetChanged();
                            } else {
                                mList.addAll(list);
                                adapter.notifyDataSetChanged();
                            }
                        } else {
                            recyclerView.setLoadMoreEnable(false);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void showInputKeyboard() {
        inputLayout.setVisibility(View.VISIBLE);
        inputText.setHint("回复 " + pingLun.getSend_user_nickname());
        showInput2(MineMsgZanActivity.this, inputText);

    }

    private void hideInputKeyboard() {
        inputText.getText().delete(0, inputText.getText().toString().length());
        inputText.setHint("");
        inputLayout.setVisibility(View.GONE);
        hideInput(MineMsgZanActivity.this);
    }

    private void initView() {
        inputLayout = findViewById(R.id.inuput_layout);
        inputText = findViewById(R.id.input_text);
        recyclerView = (SwipeRecyclerView) findViewById(R.id.recycler);
        recyclerView.getSwipeRefreshLayout()
                .setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        setRecycleView(recyclerView);
        recyclerView.setRefreshing(true);

        adapter = new MineMsgZanAdapter(this, mList);
        recyclerView.setAdapter(adapter);
    }

    private void addZan() {
        if (currentPingLun == null) {
            return;
        }
        //商品
        int class_id = 0;
        long relation_id = currentPingLun.getRelation_id();
        //点赞的评论ID（如果是内容点赞此处为0）
        int relation_comment_id = currentPingLun.getReply_comment_id();
        OkHttpRequest.getInstance().addCollection(HttpContants.addCollection,
                AppManager.getUserInfo().getUser_id(),
                relation_id,
                class_id,
                relation_comment_id,
                new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                    }
                });
    }

    /**
     * 回复评论
     * 二级评论
     *
     * @param com_id 被回复的评论id
     */
    private void replyComment(String content, int com_id) {
        if (class_id == 0) {
            addProductComment(content, com_id);
        } else if (class_id == 1) {
            addVideoComment(content,com_id);
        } else if (class_id == 2) {
            addAriticleComment(content, com_id);
        }
    }

    private void addAriticleComment(String content, final int parentCommentId) {
        OkHttpRequest.getInstance().addArticleComment(HttpContants.article_addComment, AppManager.getUserInfo().getUser_id(), relation_id,
                parentCommentId, content, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject articleObject = new JSONObject(response);
                            if (articleObject != null && articleObject.has(HttpContants.STATE) && articleObject.getInt(HttpContants.STATE) == 10000) {
                                pageIndex = 1;
                                initData();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 添加评论
     *
     * @param content         评论内容
     * @param parentCommentId 父评论id （0 没有，其他 有）
     */
    private void addProductComment(String content, final int parentCommentId) {
        OkHttpRequest.getInstance().addProductComment(HttpContants.product_addComment, AppManager.getUserInfo().getUser_id(), relation_id,
                parentCommentId, content, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
//                                pageIndex = 1;
//                                initData();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void addVideoComment(String comment, int parent_comment_id) {
        OkHttpRequest.getInstance().addVideoComment(HttpContants.video_addComment, AppManager.getUserInfo().getUser_id(),
                relation_id, parent_comment_id, comment, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
//                                pageIndex = 1;
//                                initData();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
